{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "a6220dbc-9001-47ae-bad8-6019e79cec9e",
   "metadata": {},
   "source": [
    "# MathQA Dataset, Controll Confounder"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "1e21e21d-9045-4484-8ffd-6cb0b4799c1d",
   "metadata": {},
   "outputs": [],
   "source": [
    "import openai\n",
    "import re\n",
    "import json\n",
    "import spacy\n",
    "\n",
    "import numpy as np\n",
    "\n",
    "from tqdm import tqdm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "b5cac5fb-688d-4ac5-9775-b9d08f9bdb3f",
   "metadata": {},
   "outputs": [],
   "source": [
    "nlp = spacy.load(\"en_core_web_sm\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "45e13f43-7d78-414a-a1ab-28f78d928ac6",
   "metadata": {},
   "outputs": [],
   "source": [
    "train_data = json.load(open('MathQA/train.json'))\n",
    "dev_data = json.load(open('MathQA/dev.json'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "f5ed4e2d-4826-41bd-9f10-4e2fbb823f0e",
   "metadata": {},
   "outputs": [],
   "source": [
    "dev_200_idx = np.load('MathQA/dev_200_idx.npy')\n",
    "dev_data_200 = []\n",
    "for i in dev_200_idx: dev_data_200.append(dev_data[i])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "529b57ef-7617-4f1b-8629-147b07b63d5e",
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_step(sent):\n",
    "    doc = nlp(sent)\n",
    "    n_step = len(list(s for s in doc.sents))\n",
    "    return n_step\n",
    "\n",
    "def get_ans_dict(c):\n",
    "    if(c.startswith(\"['\")):\n",
    "        c_ = eval(c)\n",
    "        ans_dict = {}\n",
    "        for ac in c_:\n",
    "            idx_, num_ = ac.split(' ) ')[-2:]\n",
    "            if('a' in idx_): ans_dict['a'] = num_\n",
    "            if('b' in idx_): ans_dict['b'] = num_\n",
    "            if('c' in idx_): ans_dict['c'] = num_\n",
    "            if('d' in idx_): ans_dict['d'] = num_\n",
    "            if('e' in idx_): ans_dict['e'] = num_\n",
    "    else:\n",
    "        ans_dict = {}\n",
    "        pattern = ' , [a-e] \\) '\n",
    "        c_ = re.split(pattern, c)\n",
    "        c_[0] = c_[0].split(' ) ')[-1]\n",
    "        for i, c in zip(['a', 'b', 'c', 'd', 'e'], c_):\n",
    "            ans_dict[i] = c\n",
    "    return ans_dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "ae3c803a-4924-45f4-889f-d685c6843635",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test_answer(pred_str, ans_str):\n",
    "    \"\"\"Find the last number as the predicted answer\"\"\"\n",
    "    pattern = '\\d*\\.?\\d+'\n",
    "    pred = re.findall(pattern, pred_str)\n",
    "    if(len(pred) >= 1):\n",
    "        # print(pred_str)\n",
    "        pred = pred[-1]\n",
    "        gold = re.findall(pattern, ans_str)\n",
    "        # print(ans_str)\n",
    "        gold = gold[-1]\n",
    "        return pred == gold\n",
    "    else: return False\n",
    "\n",
    "def parse_pred_ans(filename):\n",
    "    with open(filename) as fd: lines = fd.readlines()\n",
    "    am, a = None, None\n",
    "    num_q, acc = 0, 0\n",
    "    current_mode = 'none'\n",
    "    questions = []\n",
    "    ans_pred = []\n",
    "    ans_gold = []\n",
    "    for l in lines:\n",
    "        if(l.startswith('Question: ')):\n",
    "            if(current_mode != 'none' and current_mode != 'a'):\n",
    "                # print(l)\n",
    "                current_mode = 'none'\n",
    "                continue\n",
    "            if(am is not None and a is not None):\n",
    "                questions.append(q)\n",
    "                ans_pred.append(am)\n",
    "                ans_gold.append(a)\n",
    "                if(test_answer(am, a)):\n",
    "                    acc += 1\n",
    "            current_mode = 'q'\n",
    "            q = l\n",
    "            num_q += 1\n",
    "        elif(l.startswith('Options: ')): pass\n",
    "        elif(l.startswith('A_model:')):\n",
    "            current_mode = 'am'\n",
    "            am = l\n",
    "        elif(l.startswith('A:')):\n",
    "            current_mode = 'a'\n",
    "            a = l\n",
    "        else:\n",
    "            if(current_mode == 'q'): q += l\n",
    "            elif(current_mode == 'am'): am += l\n",
    "            elif(current_mode == 'a'): a += l\n",
    "            elif(current_mode == 'none'): pass\n",
    "            else:\n",
    "                raise ValueError(current_mode)\n",
    "                \n",
    "    questions.append(q)\n",
    "    ans_pred.append(am)\n",
    "    ans_gold.append(a)\n",
    "    if(test_answer(am, a)):\n",
    "        acc += 1\n",
    "    print('num_q %d correct %d ratio %.4f' % (num_q, acc, float(acc / num_q)))\n",
    "    return questions, ans_pred, ans_gold"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "id": "0bd48878-5577-4ead-b5a0-0b65e21ebd81",
   "metadata": {},
   "outputs": [],
   "source": [
    "def per_step_len(prompt):\n",
    "    total_len = []\n",
    "    for l in prompt.split('\\n'):\n",
    "        if((not l.startswith('Question')) and (not l.startswith(\"Let's think\")) and (not l.startswith('The answer is')) and (not l.startswith(\"Options\")) and l != ''):\n",
    "            total_len.append(len(l))\n",
    "    return np.average(total_len), len(total_len)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ea665532-0af4-463b-8a40-d43c573eac1c",
   "metadata": {},
   "source": [
    "# Number of Reasoning Step"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "5f9b433a-7393-48ef-8f09-cf8a0778bdeb",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 29837/29837 [04:59<00:00, 99.56it/s]\n"
     ]
    }
   ],
   "source": [
    "train_steps = []\n",
    "for d in tqdm(train_data): train_steps.append(get_step(d['Rationale']))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "70f5f8d6-504b-4c03-8549-c15d30628ca9",
   "metadata": {},
   "outputs": [],
   "source": [
    "train_step_dict = {}\n",
    "for i, ti in enumerate(train_steps):\n",
    "    if ti in train_step_dict: train_step_dict[ti].append(i)\n",
    "    else: train_step_dict[ti] = [i]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "c6e36d93-35be-44f7-ba1f-51dffdacf9a2",
   "metadata": {},
   "outputs": [],
   "source": [
    "prompt_easy = open('MathQA/prompt_easy.txt').read()\n",
    "prompt_complex = open('MathQA/prompt_complex.txt').read()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "81c9d4b3-73a9-43fd-b98f-dcbdecf2716f",
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Question: on a purchase of $ 130 , a store offered a payment plan consisting of a $ 30 down payment and 12 monthly payments of $ 10 each . what percent of the purchase price , to the nearest tenth of a percent , did the customer pay in interest by using this plan ?\n",
      "Options: a ) 23.5 % , b ) 24.5 % , c ) 23.1 % , d ) 25.5 % , e ) 26.5 %\n",
      "Let's think step by step\n",
      "12 * 10 + 30 = 150.\n",
      "( 30 / 130 ) * 100 = 23.1\n",
      "The answer is c, 23.1 %\n",
      "\n",
      "Question: the ratio of three numbers is 1 : 3 : 4 and the sum of their squares is 2600 . the sum of the numbers is ?\n",
      "Options: a ) 10 , b ) 12 , c ) 15 , d ) 14 , e ) 9\n",
      "Let's think step by step\n",
      "let the numbers be x , 3 x , 4 x then , x^2 + 9x^2 + 16x^2 = 2600\n",
      "26 x^2 = 2600, x^2 = 100, x = 10\n",
      "The answer is a, 10\n",
      "\n",
      "Question: the average weight of 8 person ' s increases by 1.5 kg when a new person comes in place of one of them weighing 75 kg . what might be the weight of the new person ?\n",
      "Options: a ) 81 kg , b ) 85 kg , c ) 90 kg , d ) 100 kg , e ) 110 kg\n",
      "Let's think step by step\n",
      "total weight increased = ( 8 x 1.5 ) kg = 6 kg\n",
      "weight of new person = ( 75 + 6 ) kg = 81 kg.\n",
      "The answer is a, 81 kg\n",
      "\n",
      "Question: how many positive integers less than 60 have a reminder 01 when divided by 3 ?\n",
      "Options: a ) 13 , b ) 14 , c ) 15 , d ) 16 , e ) 20\n",
      "Let's think step by step\n",
      "1 also gives the remainder of 1 when divided by 3 .\n",
      "so , there are total of 20 numbers .\n",
      "The answer is e, 20\n",
      "\n",
      "Question: 8 is 2 % of a , and 2 is 8 % of b . c equals b / a . what is the value of c ?\n",
      "Options: a ) 1 / 16 , b ) 1 / 4 , c ) 1 , d ) 4 , e ) 8\n",
      "Let's think step by step\n",
      "2a / 100 = 8, a = 400\n",
      "8b / 100 = 2, b = 25\n",
      "c = b / a = 25 / 400 = 1 / 16\n",
      "The answer is a, 1 / 16\n",
      "\n",
      "Question: the membership of a committee consists of 3 english teachers , 4 mathematics teachers , and 2 social studies teachers . if 2 committee members are to be selected at random to write the committee ’ s report , what is the probability that the two members selected will both be maths teachers ?\n",
      "Options: a ) 2 / 3 , b ) 1 / 3 , c ) 2 / 9 , d ) 1 / 24 , e ) 1 / 12\n",
      "Let's think step by step\n",
      "probability of first member an english teacher = 3 / 9\n",
      "probability of second member an english teacher = 2 / 8\n",
      "probability of both being english teacher = 3 / 9 x 2 / 8 = 1 / 12\n",
      "The answer is e, 1 / 12\n",
      "\n",
      "Question: the radius of a cylindrical vessel is 7 cm and height is 5 cm . find the whole surface of the cylinder ?\n",
      "Options: a ) 281 , b ) 528 , c ) 440 , d ) 767 , e ) 1981\n",
      "Let's think step by step\n",
      "r = 7, h = 5\n",
      "2 π r ( h + r ) = 2 * 22 / 7 * 7 ( 12 ) = 528\n",
      "The answer is b, 528\n",
      "\n",
      "Question: dan ’ s car gets 32 miles per gallon . if gas costs $ 4 / gallon , then how many miles can dan ’ s car go on $ 58 of gas ?\n",
      "Options: a ) 242 , b ) 353 , c ) 464 , d ) 575 , e ) 686\n",
      "Let's think step by step\n",
      "58 / 4 = 14.5 gallons\n",
      "14.5 * 32 = 464 miles\n",
      "the answer is c, 464\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print(prompt_easy)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "3e4a180f-5337-4c29-8965-5dba2b230ac3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "18"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(prompt_easy.split('\\n')) - 5 * 8"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "84b56e94-1b0d-410c-b5ef-0dbd1ea4b6c3",
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Question: there were 35 students in a hostel . due to the admission of 7 new students the expenses of the mess were increased by rs . 84 per day while the average expenditure per head diminished by re 1 . what was the original expenditure of the mess ?\n",
      "Options: a ) rs 450 , b ) rs 920 , c ) rs 550 , d ) rs . 630 , e ) none of these\n",
      "Let's think step by step\n",
      "let the original average expenditure be rs . x\n",
      "then , 42 ( x - 1 ) - 35 x = 84\\,7 x = 126\n",
      "x = 18\n",
      "therefore original expenditure = rs . ( 35 * 18 ) = rs . 630.\n",
      "The answer is d, rs . 630\n",
      "\n",
      "Question: a train 200 m long passes a man , running at 5 km / hr in the same direction in which the train is going , in 10 seconds . the speed of the train is ?\n",
      "Options: a ) 28 , b ) 50 , c ) 77 , d ) 22 , e ) 12\n",
      "Let's think step by step\n",
      "speed of the train relative to man = ( 200 / 10 ) m / sec = ( 20 ) m / sec.\n",
      "[ ( 20 ) * ( 18 / 5 ) ] km / hr = 72 km / hr.\n",
      "let the speed of the train be x km / hr.\n",
      "then , relative speed = ( x - 5 ) km / hr.\n",
      "x - 5 = 72, x = 77 km / hr .\n",
      "The answer is c, 77\n",
      "\n",
      "Question: solution x contains 20 % of material a and 80 % of material b . solution y contains 30 % of material a and 70 % of material b . a mixture of both these solutions contains 22 % of material a in the final product . how much solution x is present in the mixture ?\n",
      "Options: a ) 40 % , b ) 60 % , c ) 80 % , d ) 100 % , e ) 110 %\n",
      "Let's think step by step\n",
      "we can assume the total weight of the mixture = 100\n",
      "conc of a in the final mixture = 22\n",
      "let weight of a in the mixture be x.\n",
      "conc given = 20% = 0.2\n",
      "therefore , weight of b = 100 - x.\n",
      "conc given = 30% = 0.3\n",
      "now , accordding to the problem, 0.2 x + 0.3 ( 100 - x ) = 22\n",
      "solving , we get x = 80\n",
      "since we assumed the weight of the mixture = 100, therefore presence of a in the mixture = 80%.\n",
      "The answer is c, 80%\n",
      "\n",
      "Question: a trader sells 40 metres of cloth for rs . 8200 at a profit of rs . 35 per metre of cloth . how much profit will the trder earn on 40 metres of cloth ?\n",
      "Options: a ) rs . 950 , b ) rs . 1500 , c ) rs . 1000 , d ) rs . 1400 , e ) none of these\n",
      "Let's think step by step\n",
      "price of 1 metre cloth = 8200 / 40 = rs 205\n",
      "cost of 1 metre cloth = rs 205 – 35 = rs 170\n",
      "cost on 40 metres = 170 x 40 = rs . 6800\n",
      "profit earned on 40 metres cloth = rs . 8200 – rs . 6800 = rs . 1400\n",
      "The answer is d, rs . 1400\n",
      "\n",
      "Question: if x < y < z and y - x > 5 , where x is an even integer and y and z are odd integers , what is the least possible value s of z - x ?\n",
      "Options: a ) 6 , b ) 7 , c ) 8 , d ) 9 , e ) 10\n",
      "Let's think step by step\n",
      "We know x < y < z\n",
      "to find the least possible value for z - x, we need to find the values for z and x that can be closest to each other.\n",
      "if x is some even number, then what could be minimum possible odd z.\n",
      "if x is some even number, y - x > 5 ; y > x + 5\n",
      "minimum value for y = x + 5 + 2 = x + 7\n",
      "(note : x + 5 is as even + odd = odd and nearest odd greater than x + 5 is x + 5 + 2)\n",
      "minimum value for z = y + 2 = x + 7 + 2 = x + 9\n",
      "(note : z = y + 2 because both z and y are odd, difference between two odd numbers is 2)\n",
      "s = z - x = x + 9 - x = 9\n",
      "The answer is d, 9\n",
      "\n",
      "Question: what is the difference between the c . i . on rs . 6000 for 1 1 / 2 years at 4 % per annum compounded yearly and half - yearly ?\n",
      "Options: a ) s . 2.04 , b ) s . 2.08 , c ) s . 2.02 , d ) s . 2.83 , e ) s . 2.45\n",
      "Let's think step by step\n",
      "c . i . when interest is compounded yearly = [ 6000 * ( 1 + 4 / 100 ) * ( 1 + ( 1 / 2 * 4 ) / 100 ] = 6000 * 26 / 25 * 51 / 50 = rs . 6364.8\n",
      "c . i . when interest is compounded half - yearly = [ 6000 * ( 1 + 2 / 100 ) 2 ] = ( 6000 * 51 / 50 * 51 / 50 * 51 / 50 ) = rs . 6367.25\n",
      "difference = ( 6367.25 - 6364.8 ) = rs . 2.45.\n",
      "The answer is e, s . 2.45\n",
      "\n",
      "Question: the average weight of a , b and c is 45 kg . if the average weight of a and b be 40 kg and that of b and c be 45 kg , then the weight of b is :\n",
      "Options: a ) 31 kg , b ) 32 kg , c ) 33 kg , d ) 35 kg , e ) none of theseLet's think step by step\n",
      "let a , b , c represent their respective weights.\n",
      "then , we have : a + b + c = ( 45 x 3 ) = 135 … ( i )\n",
      "a + b = ( 40 x 2 ) = 80 … ( ii )\n",
      "b + c = ( 45 x 2 ) = 90 … ( iii )\n",
      "adding ( ii ) and ( iii ), we get : a + 2 b + c = 170 … ( iv )\n",
      "subtracting ( i ) from ( iv ), we get : b = 35.\n",
      "b’ s weight = 35 kg .\n",
      "The answer is d 35 kg\n",
      "\n",
      "Question: the compound and the simple interests on a certain sum at the same rate of interest for two years are rs . 11730 and rs . 10200 respectively . find the sum\n",
      "Options: a ) rs . 17037 , b ) rs . 17000 , c ) rs . 17276 , d ) rs . 170287 , e ) rs . 171881\n",
      "Let's think step by step\n",
      "the simple interest for the first year is 10200 / 2 is rs . 5100\n",
      "and compound interest for first year also is rs . 5100.\n",
      "the compound interest for second year on rs . 5100 for one year\n",
      "so rate of the interest = ( 100 * 1530 ) / ( 5100 * 1 ) = 30% p\n",
      "so p = ( 100 * 10200 ) / ( 30 * 2 ) = rs . 17000\n",
      "The answer is b, rs . 17000\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print(prompt_complex)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "b478ebb4-ebd5-4f7e-8a42-0351fb1c5c0e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "45"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(prompt_complex.split('\\n')) - 5 * 8"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "389cc1cb-8e28-4e1a-a459-1d55c97d1a5e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# easier_idx = np.random.choice(len(train_step_dict[1]), 20, replace=False)\n",
    "easier_idx = np.array([5096, 4410, 4026, 3846, 5904,  689, 4509,  929, 6526, 2796, 5440,\n",
    "       3127, 5012,  343, 2842, 3312,  750, 7139, 7064, 4374])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "7ff9e1cc-1e86-4bfd-95cb-bbaff1e1238a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([5096, 4410, 4026, 3846, 5904,  689, 4509,  929, 6526, 2796, 5440,\n",
       "       3127, 5012,  343, 2842, 3312,  750, 7139, 7064, 4374])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "easier_idx"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "0c04d725-6797-4003-b861-3c4879033f76",
   "metadata": {},
   "outputs": [],
   "source": [
    "prompt_easy_more_cases = ''\n",
    "\n",
    "for ei in easier_idx:\n",
    "    idx = train_step_dict[1][ei]\n",
    "    q = train_data[idx]['Problem']\n",
    "    c = train_data[idx]['options']\n",
    "    cr = train_data[idx]['correct']\n",
    "    r = train_data[idx]['Rationale']\n",
    "    \n",
    "    ans_dict = {}\n",
    "    for ac in c.split(', '):\n",
    "        idx_, num_ = ac.split(' ) ')[-2:]\n",
    "        if('a' in idx_): ans_dict['a'] = num_\n",
    "        if('b' in idx_): ans_dict['b'] = num_\n",
    "        if('c' in idx_): ans_dict['c'] = num_\n",
    "        if('d' in idx_): ans_dict['d'] = num_\n",
    "        if('e' in idx_): ans_dict['e'] = num_\n",
    "        \n",
    "    if(r[0] == '\"'): r = r[1:]\n",
    "    if(r[-1] == '\"'): r = r[:-1]\n",
    "    \n",
    "    r += ' ' + ans_dict[cr]\n",
    "    \n",
    "    prompt_easy_more_cases += 'Question: ' + q + '\\n'\n",
    "    prompt_easy_more_cases += 'Options: ' + c + '\\n'\n",
    "    prompt_easy_more_cases += \"Let's think step by step\\n\"\n",
    "    prompt_easy_more_cases += r + '\\n\\n'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "b467c42c-28b6-45cb-8633-bc7402a17a43",
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Question: how many seconds will a 500 meter long train take to cross a man walking with a speed of 3 km / hr in the direction of the moving train if the speed of the train is 63 km / hr\n",
      "Options: a ) 25 seconds , b ) 28 seconds , c ) 30 seconds , d ) 35 seconds , e ) 40 seconds\n",
      "Let's think step by step\n",
      "explanation : relative speed = 63 - 3 = 60 km / hr = 60 * ( 5 / 18 ) = 50 / 3 m / sec time taken to pass the man will be 500 â ˆ — 3 / 50 = 30 seconds answer is c 30 seconds \n",
      "\n",
      "Question: the side of a rhombus is 28 m and length of one of its diagonals is 12 m . the area of the rhombus is ?\n",
      "Options: a ) 326.2 , b ) 324.2 , c ) 328.2 , d ) 338.2 , e ) 328.9\n",
      "Let's think step by step\n",
      "area of the rhombus = 1 / 2 * p * √ 4 ( a ) 2 - ( p ) 2 a = 28 ; p = 12 a = 1 / 2 * 12 * √ 4 ( 28 ) 2 - ( 12 ) 2 = 1 / 2 * 12 * √ 3136 - 144 = 1 / 2 * 12 * √ 2992 a = 328.2 answer : c 328.2 \n",
      "\n",
      "Question: if seven machines working at the same rate can do 3 / 4 of a job in 30 minutes , how many minutes would it take two machines working at the same rate to do 3 / 5 of the job ?\n",
      "Options: a ) 45 , b ) 60 , c ) 75 , d ) 84 , e ) 100\n",
      "Let's think step by step\n",
      "using the std formula m 1 d 1 h 1 / w 1 = m 2 d 2 h 2 / w 2 substituting the values we have 7 * 1 / 2 * 4 / 3 = 2 * 5 / 3 * x ( converted 30 min into hours = 1 / 2 ) 14 / 3 = 10 / 3 * x x = 7 / 5 hour so 84 minutes answer : d 84 \n",
      "\n",
      "Question: a person can swim in still water at 10 km / h . if the speed of water 4 km / h , how many hours will the man take to swim back against the current for 12 km ?\n",
      "Options: a ) 2 , b ) 7 , c ) 5 , d ) 9 , e ) 6\n",
      "Let's think step by step\n",
      "m = 10 s = 4 us = 10 - 4 = 6 d = 12 t = 12 / 6 = 2 answer : a 2 \n",
      "\n",
      "Question: if the operation ø is defined for all positive integers x and w by x ø w = ( 2 ^ x ) / ( 2 ^ w ) then ( 4 ø 2 ) ø 3 = ?\n",
      "Options: a ) 2 , b ) 4 , c ) 8 , d ) 16 , e ) 32\n",
      "Let's think step by step\n",
      "4 ø 2 = 2 ^ 4 / 2 ^ 2 = 4 4 ø 3 = 2 ^ 4 / 2 ^ 3 = 2 the answer is a . 2 \n",
      "\n",
      "Question: the ratio of the incomes of rajan and balan is 7 : 6 and the ratio of their expenditure is 6 : 5 . if at the end of the year , each saves $ 1000 then the income of rajan is ?\n",
      "Options: a ) $ 7600 , b ) $ 7000 , c ) $ 8000 , d ) $ 9000 , e ) $ 5000\n",
      "Let's think step by step\n",
      "let the income of rajan and balan be $ 7 x and $ 6 x let their expenditures be $ 6 y and $ 5 y 7 x - 6 y = 1000 - - - - - - - 1 ) 6 x - 5 y = 1000 - - - - - - - 2 ) from 1 ) and 2 ) x = 1000 rajan ' s income = 7 x = 7 * 1000 = $ 7000 answer is b $ 7000 \n",
      "\n",
      "Question: andy solves problems 80 to 125 inclusive in a math exercise . how many problems does he solve ?\n",
      "Options: a ) 53 , b ) 52 , c ) 51 , d ) 46 , e ) 49\n",
      "Let's think step by step\n",
      "125 - 80 + 1 = 52 ' d ' is the answer 46 \n",
      "\n",
      "Question: a certain lab experiments with white and brown mice only . in one experiment , 3 / 7 of the mice are white . if therare 24 brown mice in the experiment , how many mice in total are in the experiment ?\n",
      "Options: a ) 14 , b ) 16 , c ) 18 , d ) 12 , e ) 20\n",
      "Let's think step by step\n",
      "let total number of mice = m number of white mice = 3 / 7 m number of brown mice = 4 / 3 m = 18 = > m = 18 answer c 18 \n",
      "\n",
      "Question: if there are 56 laborers in a crew , and on a certain day , 30 were present . calculate the percentage that showed up for work ? ( round to the nearest tenth ) .\n",
      "Options: a ) 53.6 % , b ) 52.6 % , c ) 54.6 % , d ) 55.6 % , e ) 59.6 %\n",
      "Let's think step by step\n",
      "30 / 56 * 100 = 53.57 53.6 % correct answer a 53.6 % \n",
      "\n",
      "Question: a man walking at a rate of 10 km / hr crosses a bridge in 3 minutes . the length of the bridge is ?\n",
      "Options: a ) 500 , b ) 650 , c ) 250 , d ) 111 , e ) 236\n",
      "Let's think step by step\n",
      "speed = 10 * 5 / 18 = 50 / 18 m / sec distance covered in 5 minutes = 50 / 18 * 5 * 60 = 500 m answer is a 500 \n",
      "\n",
      "Question: what is the remainder when 7 ^ 74 - 5 ^ 74 is divided by 12 ?\n",
      "Options: a ) 1 , b ) 0 , c ) 2 , d ) 3 , e ) none of these\n",
      "Let's think step by step\n",
      "easiest way for me : 7 ^ 74 - 5 ^ 74 = ( 49 ) ^ 37 - 25 ^ 37 = ( 12 * 4 + 1 ) ^ 37 - ( 12 * 2 + 1 ) ^ 37 - > remainder is 1 ^ 37 - 1 ^ 37 = 0 ans : b 0 \n",
      "\n",
      "Question: find the value of 72518 x 9999 = m ?\n",
      "Options: a ) 456578972 , b ) 436567874 , c ) 653658791 , d ) 725117481 , e ) 725107482\n",
      "Let's think step by step\n",
      "72518 x 9999 = 72518 x ( 10000 - 1 ) = 72518 x 10000 - 72518 x 1 = 725180000 - 72518 = 725107482 e 725107482\n",
      "\n",
      "Question: what is 50 % of 40 % of 1200 grams ?\n",
      "Options: a ) 450 gms , b ) 100 gms , c ) 300 gms , d ) 240 gms , e ) none of these\n",
      "Let's think step by step\n",
      "50 / 100 × 40 / 100 × 1200 = 240 answer : d 240 gms \n",
      "\n",
      "Question: of the female students at barkely university , 1 / 6 are on the honor roll . of the male students , 2 / 5 are on the honor roll . if 3 / 5 of the students are female , what is ratio of male to female students on honor roll ?\n",
      "Options: a ) 8 / 5 , b ) 3 / 15 , c ) 23 / 30 , d ) 43 / 60 , e ) 53 / 90\n",
      "Let's think step by step\n",
      "let the total students be 100 given 3 / 5 of the students are females = 60 then males = 2 / 5 = 40 1 / 6 of the females are on honor roll = 10 males on the honor roll = 2 / 5 = 16 ratio of m : f students on honor roll = 16 / 10 = 8 / 5 a 8 / 5 \n",
      "\n",
      "Question: if an average hard drive had a capacity of 0.6 tb in 2000 , and average hard drive capacities double every 5 years , what will be the average hard drive capacity in 2050 ?\n",
      "Options: a ) 256 , b ) 512 , c ) 768 , d ) 6144 , e ) 1536\n",
      "Let's think step by step\n",
      "0.6 * 2 ^ 10 = 0.6 * 1024 = 614.4 the answer is d . 6144 \n",
      "\n",
      "Question: a hall 36 m long and 15 m broad is to be paved with stones , each measuring 8 dm by 5 dm . the number of stones required is :\n",
      "Options: a ) 180 , b ) 1800 , c ) 18 , d ) 18000 , e ) 1350\n",
      "Let's think step by step\n",
      "area of the hall = 3600 * 1500 area of each stone = ( 80 * 50 ) therefore , number of stones = ( 3600 * 1500 / 80 * 50 ) = 1350 answer : e 1350\n",
      "\n",
      "Question: two pipes a and b can fill a tank in 12 and 24 minutes respectively . if both the pipes are used together , then how long will it take to fill the tank ?\n",
      "Options: a ) 6 minutes , b ) 8 minutes , c ) 5 minutes , d ) 2 minutes , e ) 7 minutes\n",
      "Let's think step by step\n",
      "required time = 12 * 24 / 12 + 12 = 12 * 24 / 36 = 24 / 3 = 8 minutes answer : b 8 minutes \n",
      "\n",
      "Question: a man complete a journey in 10 hours . he travels first half of the journey at the rate of 21 km / hr and second half at the rate of 24 km / hr . find the total journey in km .\n",
      "Options: a ) 220 km , b ) 224 km , c ) 230 km , d ) 234 km , e ) 255 km\n",
      "Let's think step by step\n",
      "( 1 / 2 ) x / 21 + ( 1 / 2 ) x / 24 = 10 x / 21 + x / 24 = 20 15 x = 168 x 20 x = [ 168 x 20 / 15 ] = 224 km answer is b 224 km \n",
      "\n",
      "Question: in a 200 meter race a beats b by 35 meters in 7 seconds . a ' s time over the cause is ?\n",
      "Options: a ) 47 sec , b ) 40 sec , c ) 45 sec , d ) 33 sec , e ) 35 sec\n",
      "Let's think step by step\n",
      "b covers 35 m in 7 seconds b take time = ( 200 * 7 ) / 35 = 40 a takes time = 40 - 7 = 33 sec answer : d 33 sec \n",
      "\n",
      "Question: 0.009 / x = 0.01 . find the value of x\n",
      "Options: a ) 0.0009 , b ) 0.09 , c ) 0.9 , d ) 9 , e ) 90\n",
      "Let's think step by step\n",
      "x = 0.009 / 0.01 = 0.9 answer : c 0.9 \n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print(prompt_easy_more_cases)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "0d871f0e-8b71-4f8c-9ab2-3e09df7be103",
   "metadata": {},
   "outputs": [],
   "source": [
    "prompt_easy_more_cases = open('MathQA/prompt_easy_more_cases.txt').read()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "a785a0f5-8b16-49af-a2ed-68ba9de21df8",
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"Question: on a purchase of $ 130 , a store offered a payment plan consisting of a $ 30 down payment and 12 monthly payments of $ 10 each . what percent of the purchase price , to the nearest tenth of a percent , did the customer pay in interest by using this plan ?\\nOptions: a ) 23.5 % , b ) 24.5 % , c ) 23.1 % , d ) 25.5 % , e ) 26.5 %\\nLet's think step by step\\n12 * 10 + 30 = 150.\\n( 30 / 130 ) * 100 = 23.1\\nThe answer is c, 23.1 %\\n\\nQuestion: the ratio of three numbers is 1 : 3 : 4 and the sum of their squares is 2600 . the sum of the numbers is ?\\nOptions: a ) 10 , b ) 12 , c ) 15 , d ) 14 , e ) 9\\nLet's think step by step\\nlet the numbers be x , 3 x , 4 x then , x^2 + 9x^2 + 16x^2 = 2600\\n26 x^2 = 2600, x^2 = 100, x = 10\\nThe answer is a, 10\\n\\nQuestion: the average weight of 8 person ' s increases by 1.5 kg when a new person comes in place of one of them weighing 75 kg . what might be the weight of the new person ?\\nOptions: a ) 81 kg , b ) 85 kg , c ) 90 kg , d ) 100 kg , e ) 110 kg\\nLet's think step by step\\ntotal weight increased = ( 8 x 1.5 ) kg = 6 kg\\nweight of new person = ( 75 + 6 ) kg = 81 kg.\\nThe answer is a, 81 kg\\n\\nQuestion: how many positive integers less than 60 have a reminder 01 when divided by 3 ?\\nOptions: a ) 13 , b ) 14 , c ) 15 , d ) 16 , e ) 20\\nLet's think step by step\\n1 also gives the remainder of 1 when divided by 3 .\\nso , there are total of 20 numbers .\\nThe answer is e, 20\\n\\nQuestion: 8 is 2 % of a , and 2 is 8 % of b . c equals b / a . what is the value of c ?\\nOptions: a ) 1 / 16 , b ) 1 / 4 , c ) 1 , d ) 4 , e ) 8\\nLet's think step by step\\n2a / 100 = 8, a = 400\\n8b / 100 = 2, b = 25\\nc = b / a = 25 / 400 = 1 / 16\\nThe answer is a, 1 / 16\\n\\nQuestion: the membership of a committee consists of 3 english teachers , 4 mathematics teachers , and 2 social studies teachers . if 2 committee members are to be selected at random to write the committee ’ s report , what is the probability that the two members selected will both be maths teachers ?\\nOptions: a ) 2 / 3 , b ) 1 / 3 , c ) 2 / 9 , d ) 1 / 24 , e ) 1 / 12\\nLet's think step by step\\nprobability of first member an english teacher = 3 / 9\\nprobability of second member an english teacher = 2 / 8\\nprobability of both being english teacher = 3 / 9 x 2 / 8 = 1 / 12\\nThe answer is e, 1 / 12\\n\\nQuestion: the radius of a cylindrical vessel is 7 cm and height is 5 cm . find the whole surface of the cylinder ?\\nOptions: a ) 281 , b ) 528 , c ) 440 , d ) 767 , e ) 1981\\nLet's think step by step\\nr = 7, h = 5\\n2 π r ( h + r ) = 2 * 22 / 7 * 7 ( 12 ) = 528\\nThe answer is b, 528\\n\\nQuestion: dan ’ s car gets 32 miles per gallon . if gas costs $ 4 / gallon , then how many miles can dan ’ s car go on $ 58 of gas ?\\nOptions: a ) 242 , b ) 353 , c ) 464 , d ) 575 , e ) 686\\nLet's think step by step\\n58 / 4 = 14.5 gallons\\n14.5 * 32 = 464 miles\\nThe answer is c, 464\\n\\nQuestion: how many seconds will a 500 meter long train take to cross a man walking with a speed of 3 km / hr in the direction of the moving train if the speed of the train is 63 km / hr\\nOptions: a ) 25 seconds , b ) 28 seconds , c ) 30 seconds , d ) 35 seconds , e ) 40 seconds\\nLet's think step by step\\nrelative speed = 63 - 3 = 60 km / hr = 60 * ( 5 / 18 ) = 50 / 3 m / sec \\ntime taken to pass the man will be 500 â ˆ — 3 / 50 = 30 seconds \\nThe answer is c, 30 seconds \\n\\nQuestion: the side of a rhombus is 28 m and length of one of its diagonals is 12 m . the area of the rhombus is ?\\nOptions: a ) 326.2 , b ) 324.2 , c ) 328.2 , d ) 338.2 , e ) 328.9\\nLet's think step by step\\narea of the rhombus = 1 / 2 * p * √ 4 ( a ) 2 - ( p ) 2 a = 28 ; \\np = 12, a = 1 / 2 * 12 * √ 4 ( 28 ) 2 - ( 12 ) 2 = 1 / 2 * 12 * √ 3136 - 144 = 1 / 2 * 12 * √ 2992 a = 328.2 \\nThe answer is c, 328.2 \\n\\nQuestion: if seven machines working at the same rate can do 3 / 4 of a job in 30 minutes , how many minutes would it take two machines working at the same rate to do 3 / 5 of the job ?\\nOptions: a ) 45 , b ) 60 , c ) 75 , d ) 84 , e ) 100\\nLet's think step by step\\nusing the std formula m 1 d 1 h 1 / w 1 = m 2 d 2 h 2 / w 2 \\nsubstituting the values we have 7 * 1 / 2 * 4 / 3 = 2 * 5 / 3 * x \\n( converted 30 min into hours = 1 / 2 ) 14 / 3 = 10 / 3 * x x = 7 / 5 hour so 84 minutes \\nThe answer is d, 84 \\n\\nQuestion: a person can swim in still water at 10 km / h . if the speed of water 4 km / h , how many hours will the man take to swim back against the current for 12 km ?\\nOptions: a ) 2 , b ) 7 , c ) 5 , d ) 9 , e ) 6\\nLet's think step by step\\nm = 10 s = 4 \\nus = 10 - 4 = 6 \\nd = 12 t = 12 / 6 = 2 \\nThe answer is a, 2 \\n\\nQuestion: if the operation ø is defined for all positive integers x and w by x ø w = ( 2 ^ x ) / ( 2 ^ w ) then ( 4 ø 2 ) ø 3 = ?\\nOptions: a ) 2 , b ) 4 , c ) 8 , d ) 16 , e ) 32\\nLet's think step by step\\n4 ø 2 = 2 ^ 4 / 2 ^ 2 = 4 \\n4 ø 3 = 2 ^ 4 / 2 ^ 3 = 2 \\nThe answer is a, 2 \\n\\nQuestion: the ratio of the incomes of rajan and balan is 7 : 6 and the ratio of their expenditure is 6 : 5 . if at the end of the year , each saves $ 1000 then the income of rajan is ?\\nOptions: a ) $ 7600 , b ) $ 7000 , c ) $ 8000 , d ) $ 9000 , e ) $ 5000\\nLet's think step by step\\nlet the income of rajan and balan be $ 7 x and $ 6 x \\nlet their expenditures be $ 6 y and $ 5 y 7 x - 6 y = 1000 - - - - - - - 1 )\\n6 x - 5 y = 1000 - - - - - - - 2 ) \\nfrom 1 ) and 2 ) x = 1000 \\nrajan ' s income = 7 x = 7 * 1000 = $ 7000 \\nThe answer is b, $ 7000 \\n\\nQuestion: andy solves problems 80 to 125 inclusive in a math exercise . how many problems does he solve ?\\nOptions: a ) 53 , b ) 52 , c ) 51 , d ) 46 , e ) 49\\nLet's think step by step\\n125 - 80 + 1 = 52 \\nThe answer d, 46 \\n\\nQuestion: a certain lab experiments with white and brown mice only . in one experiment , 3 / 7 of the mice are white . if therare 24 brown mice in the experiment , how many mice in total are in the experiment ?\\nOptions: a ) 14 , b ) 16 , c ) 18 , d ) 12 , e ) 20\\nLet's think step by step\\nlet total number of mice = m \\nnumber of white mice = 3 / 7 m \\nnumber of brown mice = 4 / 3 m = 18\\n= > m = 18 \\nThe answer is c, 18 \\n\\nQuestion: if there are 56 laborers in a crew , and on a certain day , 30 were present . calculate the percentage that showed up for work ? ( round to the nearest tenth ) .\\nOptions: a ) 53.6 % , b ) 52.6 % , c ) 54.6 % , d ) 55.6 % , e ) 59.6 %\\nLet's think step by step\\n30 / 56 * 100 = 53.57 \\n53.6 % \\nThe answer is a, 53.6 % \\n\\nQuestion: a man walking at a rate of 10 km / hr crosses a bridge in 3 minutes . the length of the bridge is ?\\nOptions: a ) 500 , b ) 650 , c ) 250 , d ) 111 , e ) 236\\nLet's think step by step\\nspeed = 10 * 5 / 18 = 50 / 18 m / sec \\ndistance covered in 5 minutes = 50 / 18 * 5 * 60 = 500 m \\nThe answer is a, 500 \\n\\nQuestion: what is the remainder when 7 ^ 74 - 5 ^ 74 is divided by 12 ?\\nOptions: a ) 1 , b ) 0 , c ) 2 , d ) 3 , e ) none of these\\nLet's think step by step\\neasiest way for me : 7 ^ 74 - 5 ^ 74 = ( 49 ) ^ 37 - 25 ^ 37 = ( 12 * 4 + 1 ) ^ 37 - ( 12 * 2 + 1 ) ^ 37 - > \\nremainder is 1 ^ 37 - 1 ^ 37 = 0 \\nThe answer is b, 0 \\n\""
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "prompt_easy_more_cases"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "5009234c-acfc-44ad-b140-e325c66e7975",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "19\n"
     ]
    }
   ],
   "source": [
    "n_cases = 0\n",
    "for l in prompt_easy_more_cases.split('\\n'): \n",
    "    if(l == ''): n_cases += 1\n",
    "print(n_cases)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "e2f862bb-095b-4b25-b46e-cc966e88244e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "46"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(prompt_easy_more_cases.split('\\n')) - 5 * 19"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "bec78177-180d-41b6-b000-153a7b0e747c",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 200/200 [12:32<00:00,  3.76s/it]\n"
     ]
    }
   ],
   "source": [
    "i = 0\n",
    "with open('outputs/dev_outputs_easy_more_cases.txt', 'w') as fd:\n",
    "    for case in tqdm(dev_data_200):\n",
    "        q = case['Problem']\n",
    "        c = case['options']\n",
    "        a = case['Rationale']\n",
    "        cr = case['correct']\n",
    "    \n",
    "        ans_dict = {}\n",
    "        for ac in c.split(', '):\n",
    "            idx_, num_ = ac.split(' ) ')[-2:]\n",
    "            if('a' in idx_): ans_dict['a'] = num_\n",
    "            if('b' in idx_): ans_dict['b'] = num_\n",
    "            if('c' in idx_): ans_dict['c'] = num_\n",
    "            if('d' in idx_): ans_dict['d'] = num_\n",
    "            if('e' in idx_): ans_dict['e'] = num_\n",
    "            \n",
    "        a += ' ' + ans_dict[cr]\n",
    "        \n",
    "        prompt_q = '' \n",
    "        prompt_q += 'Question: ' + q + '\\n'\n",
    "        prompt_q += 'Options: ' + c + '\\n'\n",
    "        prompt_q += \"Let's think step by step\\n\"\n",
    "        \n",
    "        prompt_full = prompt_easy_more_cases + '\\n' + prompt_q\n",
    "        response = openai.Completion.create(model=\"text-davinci-002\", \n",
    "                                            prompt=prompt_full, \n",
    "                                            temperature=0, \n",
    "                                            max_tokens=256)\n",
    "        \n",
    "        ans_model = response['choices'][0]['text']\n",
    "        fd.write('%sA_model:\\n%s\\nA:\\n%s\\n\\n' % (prompt_q, ans_model, a))\n",
    "        i += 1\n",
    "        # if(i == 2): break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "d891e9a8-131f-4102-b14d-a8bf6c1e903b",
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Question: on a purchase of $ 130 , a store offered a payment plan consisting of a $ 30 down payment and 12 monthly payments of $ 10 each . what percent of the purchase price , to the nearest tenth of a percent , did the customer pay in interest by using this plan ?\n",
      "Options: a ) 23.5 % , b ) 24.5 % , c ) 23.1 % , d ) 25.5 % , e ) 26.5 %\n",
      "Let's think step by step\n",
      "12 * 10 + 30 = 150.\n",
      "( 30 / 130 ) * 100 = 23.1\n",
      "The answer is c, 23.1 %\n",
      "\n",
      "Question: the ratio of three numbers is 1 : 3 : 4 and the sum of their squares is 2600 . the sum of the numbers is ?\n",
      "Options: a ) 10 , b ) 12 , c ) 15 , d ) 14 , e ) 9\n",
      "Let's think step by step\n",
      "let the numbers be x , 3 x , 4 x then , x^2 + 9x^2 + 16x^2 = 2600\n",
      "26 x^2 = 2600, x^2 = 100, x = 10\n",
      "The answer is a, 10\n",
      "\n",
      "Question: the average weight of 8 person ' s increases by 1.5 kg when a new person comes in place of one of them weighing 75 kg . what might be the weight of the new person ?\n",
      "Options: a ) 81 kg , b ) 85 kg , c ) 90 kg , d ) 100 kg , e ) 110 kg\n",
      "Let's think step by step\n",
      "total weight increased = ( 8 x 1.5 ) kg = 6 kg\n",
      "weight of new person = ( 75 + 6 ) kg = 81 kg.\n",
      "The answer is a, 81 kg\n",
      "\n",
      "Question: how many positive integers less than 60 have a reminder 01 when divided by 3 ?\n",
      "Options: a ) 13 , b ) 14 , c ) 15 , d ) 16 , e ) 20\n",
      "Let's think step by step\n",
      "1 also gives the remainder of 1 when divided by 3 .\n",
      "so , there are total of 20 numbers .\n",
      "The answer is e, 20\n",
      "\n",
      "Question: 8 is 2 % of a , and 2 is 8 % of b . c equals b / a . what is the value of c ?\n",
      "Options: a ) 1 / 16 , b ) 1 / 4 , c ) 1 , d ) 4 , e ) 8\n",
      "Let's think step by step\n",
      "2a / 100 = 8, a = 400\n",
      "8b / 100 = 2, b = 25\n",
      "c = b / a = 25 / 400 = 1 / 16\n",
      "The answer is a, 1 / 16\n",
      "\n",
      "Question: the membership of a committee consists of 3 english teachers , 4 mathematics teachers , and 2 social studies teachers . if 2 committee members are to be selected at random to write the committee ’ s report , what is the probability that the two members selected will both be maths teachers ?\n",
      "Options: a ) 2 / 3 , b ) 1 / 3 , c ) 2 / 9 , d ) 1 / 24 , e ) 1 / 12\n",
      "Let's think step by step\n",
      "probability of first member an english teacher = 3 / 9\n",
      "probability of second member an english teacher = 2 / 8\n",
      "probability of both being english teacher = 3 / 9 x 2 / 8 = 1 / 12\n",
      "The answer is e, 1 / 12\n",
      "\n",
      "Question: the radius of a cylindrical vessel is 7 cm and height is 5 cm . find the whole surface of the cylinder ?\n",
      "Options: a ) 281 , b ) 528 , c ) 440 , d ) 767 , e ) 1981\n",
      "Let's think step by step\n",
      "r = 7, h = 5\n",
      "2 π r ( h + r ) = 2 * 22 / 7 * 7 ( 12 ) = 528\n",
      "The answer is b, 528\n",
      "\n",
      "Question: dan ’ s car gets 32 miles per gallon . if gas costs $ 4 / gallon , then how many miles can dan ’ s car go on $ 58 of gas ?\n",
      "Options: a ) 242 , b ) 353 , c ) 464 , d ) 575 , e ) 686\n",
      "Let's think step by step\n",
      "58 / 4 = 14.5 gallons\n",
      "14.5 * 32 = 464 miles\n",
      "The answer is c, 464\n",
      "\n",
      "Question: how many seconds will a 500 meter long train take to cross a man walking with a speed of 3 km / hr in the direction of the moving train if the speed of the train is 63 km / hr\n",
      "Options: a ) 25 seconds , b ) 28 seconds , c ) 30 seconds , d ) 35 seconds , e ) 40 seconds\n",
      "Let's think step by step\n",
      "relative speed = 63 - 3 = 60 km / hr = 60 * ( 5 / 18 ) = 50 / 3 m / sec \n",
      "time taken to pass the man will be 500 â ˆ — 3 / 50 = 30 seconds \n",
      "The answer is c, 30 seconds \n",
      "\n",
      "Question: the side of a rhombus is 28 m and length of one of its diagonals is 12 m . the area of the rhombus is ?\n",
      "Options: a ) 326.2 , b ) 324.2 , c ) 328.2 , d ) 338.2 , e ) 328.9\n",
      "Let's think step by step\n",
      "area of the rhombus = 1 / 2 * p * √ 4 ( a ) 2 - ( p ) 2 a = 28 ; \n",
      "p = 12, a = 1 / 2 * 12 * √ 4 ( 28 ) 2 - ( 12 ) 2 = 1 / 2 * 12 * √ 3136 - 144 = 1 / 2 * 12 * √ 2992 a = 328.2 \n",
      "The answer is c, 328.2 \n",
      "\n",
      "Question: if seven machines working at the same rate can do 3 / 4 of a job in 30 minutes , how many minutes would it take two machines working at the same rate to do 3 / 5 of the job ?\n",
      "Options: a ) 45 , b ) 60 , c ) 75 , d ) 84 , e ) 100\n",
      "Let's think step by step\n",
      "using the std formula m 1 d 1 h 1 / w 1 = m 2 d 2 h 2 / w 2 \n",
      "substituting the values we have 7 * 1 / 2 * 4 / 3 = 2 * 5 / 3 * x \n",
      "( converted 30 min into hours = 1 / 2 ) 14 / 3 = 10 / 3 * x x = 7 / 5 hour so 84 minutes \n",
      "The answer is d, 84 \n",
      "\n",
      "Question: a person can swim in still water at 10 km / h . if the speed of water 4 km / h , how many hours will the man take to swim back against the current for 12 km ?\n",
      "Options: a ) 2 , b ) 7 , c ) 5 , d ) 9 , e ) 6\n",
      "Let's think step by step\n",
      "m = 10 s = 4 \n",
      "us = 10 - 4 = 6 \n",
      "d = 12 t = 12 / 6 = 2 \n",
      "The answer is a, 2 \n",
      "\n",
      "Question: if the operation ø is defined for all positive integers x and w by x ø w = ( 2 ^ x ) / ( 2 ^ w ) then ( 4 ø 2 ) ø 3 = ?\n",
      "Options: a ) 2 , b ) 4 , c ) 8 , d ) 16 , e ) 32\n",
      "Let's think step by step\n",
      "4 ø 2 = 2 ^ 4 / 2 ^ 2 = 4 \n",
      "4 ø 3 = 2 ^ 4 / 2 ^ 3 = 2 \n",
      "The answer is a, 2 \n",
      "\n",
      "Question: the ratio of the incomes of rajan and balan is 7 : 6 and the ratio of their expenditure is 6 : 5 . if at the end of the year , each saves $ 1000 then the income of rajan is ?\n",
      "Options: a ) $ 7600 , b ) $ 7000 , c ) $ 8000 , d ) $ 9000 , e ) $ 5000\n",
      "Let's think step by step\n",
      "let the income of rajan and balan be $ 7 x and $ 6 x \n",
      "let their expenditures be $ 6 y and $ 5 y 7 x - 6 y = 1000 - - - - - - - 1 )\n",
      "6 x - 5 y = 1000 - - - - - - - 2 ) \n",
      "from 1 ) and 2 ) x = 1000 \n",
      "rajan ' s income = 7 x = 7 * 1000 = $ 7000 \n",
      "The answer is b, $ 7000 \n",
      "\n",
      "Question: andy solves problems 80 to 125 inclusive in a math exercise . how many problems does he solve ?\n",
      "Options: a ) 53 , b ) 52 , c ) 51 , d ) 46 , e ) 49\n",
      "Let's think step by step\n",
      "125 - 80 + 1 = 52 \n",
      "The answer d, 46 \n",
      "\n",
      "Question: a certain lab experiments with white and brown mice only . in one experiment , 3 / 7 of the mice are white . if therare 24 brown mice in the experiment , how many mice in total are in the experiment ?\n",
      "Options: a ) 14 , b ) 16 , c ) 18 , d ) 12 , e ) 20\n",
      "Let's think step by step\n",
      "let total number of mice = m \n",
      "number of white mice = 3 / 7 m \n",
      "number of brown mice = 4 / 3 m = 18\n",
      "= > m = 18 \n",
      "The answer is c, 18 \n",
      "\n",
      "Question: if there are 56 laborers in a crew , and on a certain day , 30 were present . calculate the percentage that showed up for work ? ( round to the nearest tenth ) .\n",
      "Options: a ) 53.6 % , b ) 52.6 % , c ) 54.6 % , d ) 55.6 % , e ) 59.6 %\n",
      "Let's think step by step\n",
      "30 / 56 * 100 = 53.57 \n",
      "53.6 % \n",
      "The answer is a, 53.6 % \n",
      "\n",
      "Question: a man walking at a rate of 10 km / hr crosses a bridge in 3 minutes . the length of the bridge is ?\n",
      "Options: a ) 500 , b ) 650 , c ) 250 , d ) 111 , e ) 236\n",
      "Let's think step by step\n",
      "speed = 10 * 5 / 18 = 50 / 18 m / sec \n",
      "distance covered in 5 minutes = 50 / 18 * 5 * 60 = 500 m \n",
      "The answer is a, 500 \n",
      "\n",
      "Question: what is the remainder when 7 ^ 74 - 5 ^ 74 is divided by 12 ?\n",
      "Options: a ) 1 , b ) 0 , c ) 2 , d ) 3 , e ) none of these\n",
      "Let's think step by step\n",
      "easiest way for me : 7 ^ 74 - 5 ^ 74 = ( 49 ) ^ 37 - 25 ^ 37 = ( 12 * 4 + 1 ) ^ 37 - ( 12 * 2 + 1 ) ^ 37 - > \n",
      "remainder is 1 ^ 37 - 1 ^ 37 = 0 \n",
      "The answer is b, 0 \n",
      "\n",
      "Question: average of 5 numbers is 20 , and the sum of 3 of the numbers is 48 , what is the average of the other 2 numbers ?\n",
      "Options: a ) 24 , b ) 34 , c ) 29 , d ) 35 , e ) 26\n",
      "Let's think step by step\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print(prompt_full)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "b6330bec-72ee-4057-a797-891d5dcb0d29",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "num_q 201 correct 75 ratio 0.3731\n"
     ]
    }
   ],
   "source": [
    "questions, ans_pred, ans_gold =parse_pred_ans('outputs/dev_outputs_easy_more_cases.txt')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "c7346064-5fd4-4b3c-8784-6eb565731447",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "210"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(ans_gold)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8060a044-1994-4da9-b416-2b9d931fcef4",
   "metadata": {},
   "source": [
    "# Total Length"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "2a63cfb3-6f1f-403a-9b51-07fad583fc20",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 29837/29837 [00:00<00:00, 195265.78it/s]\n"
     ]
    }
   ],
   "source": [
    "train_length = []\n",
    "for d in tqdm(train_data): train_length.append(len(d['Rationale']))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "1099772f-6e86-47d4-8405-b0c4e7311698",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([167.  , 197.  , 233.  , 286.  , 398.  , 519.  , 843.64])"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.percentile(train_length, [50, 60, 70, 80, 90, 95, 99])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "fcaa955f-a76c-4fc4-ba73-e8b499076a9a",
   "metadata": {},
   "outputs": [],
   "source": [
    "prompt_longest_cases = []\n",
    "for d in train_data: \n",
    "    if(len(d['Rationale']) > 519 and len(d['Rationale']) < 800): prompt_longest_cases.append(d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "7e92658e-fb90-46f0-817d-977bf5e91156",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1099"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(prompt_longest_cases)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "4f7a196d-f3ec-4ff7-b124-b000da417d44",
   "metadata": {},
   "outputs": [],
   "source": [
    "prompt_longest = ''\n",
    "\n",
    "for d in prompt_longest_cases[:8]:\n",
    "    q = d['Problem']\n",
    "    c = d['options']\n",
    "    cr = d['correct']\n",
    "    r = d['Rationale']\n",
    "    \n",
    "    ans_dict = {}\n",
    "    for ac in c.split(', '):\n",
    "        idx_, num_ = ac.split(' ) ')[-2:]\n",
    "        if('a' in idx_): ans_dict['a'] = num_\n",
    "        if('b' in idx_): ans_dict['b'] = num_\n",
    "        if('c' in idx_): ans_dict['c'] = num_\n",
    "        if('d' in idx_): ans_dict['d'] = num_\n",
    "        if('e' in idx_): ans_dict['e'] = num_\n",
    "        \n",
    "    if(r[0] == '\"'): r = r[1:]\n",
    "    if(r[-1] == '\"'): r = r[:-1]\n",
    "    \n",
    "    r += ' ' + ans_dict[cr]\n",
    "    \n",
    "    prompt_longest += 'Question: ' + q + '\\n'\n",
    "    prompt_longest += 'Options: ' + c + '\\n'\n",
    "    prompt_longest += \"Let's think step by step\\n\"\n",
    "    prompt_longest += r + '\\n\\n'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "3097b5ae-fcd9-4f4e-8c2c-04b7bcce8975",
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Question: during a sale of 20 % on everything in a store , a kid is successful in convincing the store manager to give him 20 candies for the discounted price of 14 candies . the store still makes a profit of 18 % on this sale . what is the mark up percentage on each candy ?\n",
      "Options: a ) 100 % , b ) 80 % , c ) 75 % , d ) 66 + 2 / 3 % , e ) 55 %\n",
      "Let's think step by step\n",
      "hi akhil , i can share the way i solved it . . let ' s say marked price = m so , there ' s a discount of 20 % on m so , new s . p . = 80 % of m now , the child convinces the owner to sell 20 candies for the price of 14 candies . let ' s say each candy after discount is 1 $ . so , s . p . of 20 candies = 20 $ . the child bought it for 14 $ so , he got a discount of 6 / 20 * 100 = 30 % so , the latest s . p . = 70 % of 80 % of m = 0.7 * 0.8 m now , we are given that the shopkeeper still makes a profit of 12 % . so we have , 0.7 * 0.8 * m = 1.12 c . p so , we get , m = 2 c . p . i . e . marked price was kept 100 % above c . p . d 66 + 2 / 3 % \n",
      "\n",
      "Question: the cost of registration at a professional association meeting was $ 50 per person ; a lunch for registrants only was available for an additional $ 22 per person . if the number of registrants who paid for lunch was 50 more than the number who did not , and if receipts for registration and lunch totaled $ 82,900 , how many people paid just for registration at the meeting ?\n",
      "Options: a ) 700 , b ) 650 , c ) 1,300 , d ) 1,500 , e ) 1,800\n",
      "Let's think step by step\n",
      "hope this might be useful to you . let the number of people who have opted only to register = x now since the registration cost is 50 $ per person , the total amount sums to = 50 x $ as per the information given in the question , the number of registrants who paid for lunch was 50 more than the number who did not . that means , total number of people who registered and paid for lunch = 50 + x . for the people who registered for lunch the cost is 50 $ ( for the event registration ) + 22 $ ( for lunch ) = 72 $ . total amount in this case sums to = 72 ( 50 + x ) = 3600 + 72 x now , total amount received was 82900 . thus , from the above data , 50 x + 3600 + 72 x = 82900 122 x = 82900 - 3600 122 x = 79300 x = 650 hence the correct ans is b 650 \n",
      "\n",
      "Question: a certain clock marks every hour by striking a number of times equal to the hour , and the time require for a stroke is exactly equal to the time interval between strokes . at 6 : 00 the time lapse between the beginning of the first stoke and the end of the last stroke is 22 seconds . at 7 : 00 , how many seconds elapse between the beginning of the first stroke and the end of the last stroke ?\n",
      "Options: a ) 26 , b ) 29 , c ) 19 , d ) 30 , e ) 40\n",
      "Let's think step by step\n",
      "at 6 ' o clock , there would be 6 strikes . first strike , then a short interval , the second strike , then a short interval and so on till the 6 th strike . so there would be in all 5 intervals between 6 strikes . similarly , between 7 strikes , there would be 6 intervals . according to the question , the time spent in the strike and the interval is same . at 6 ' o clock , the 6 strikes and the 5 intervals together take 22 sec so each strike and each interval takes 2 secs . at 7 ' o clock , the 7 strikes and 6 intervals will take 2 * ( 7 + 6 ) = 26 secs a 26 \n",
      "\n",
      "Question: two vessels a and b contain spirit and water in the ratio 5 : 2 and 7 : 6 respectively . find the ratio in which these mixtures be mixed to obtain a new mixture in vessel c containing spirit and water in the ratio 8 : 5 ?\n",
      "Options: a ) 3 : 4 , b ) 4 : 3 , c ) 9 : 7 , d ) 7 : 9 , e ) 8 : 1\n",
      "Let's think step by step\n",
      "explanation : let cost price ( cp ) of 1 litre spirit be rs . 1 quantity of spirit in 1 litre mixture from vessel a = 5 / 7 cost price ( cp ) of 1 litre mixture from vessel a = rs . = 5 / 7 quantity of spirit in 1 litre mixture from vessel b = 7 / 13 cost price ( cp ) of 1 litre mixture from vessel b = rs . = 7 / 13 quantity of spirit to be obtained in 1 litre mixture from vessel c = 8 / 13 cost price ( cp ) of 1 litre mixture from vessel c ( mean price ) = rs . = 8 / 13 = > mixture from vessel a : mixture from vessel b = 1 / 13 : 9 / 91 = 7 : 9 answer is d 7 : 9 \n",
      "\n",
      "Question: a 50 kg metal bar made of alloy of tin and silver lost 5 kg of its weight in the water . 10 kg of tin loses 1.375 kg in the water ; 5 kg of silver loses 0.375 kg . what is the ratio of tin to silver in the bar ?\n",
      "Options: a ) 1 / 4 , b ) 2 / 5 , c ) 1 / 2 , d ) 3 / 5 , e ) 2 / 3\n",
      "Let's think step by step\n",
      "you can simply use this formula to avoid confusion : w 1 / w 2 = ( a 2 - aavg ) / ( avg - a 1 ) here is how you will find the values of a 1 an a 2 . we have an overall loss ( average loss ) . the average loss is 5 kg when 50 kg alloy is immersed . this is a loss of ( 5 / 50 ) * 100 = 10 % . this is aavg the loss of tin is 1.375 kg for every 10 kg . this means it loses ( 1.375 / 10 ) * 100 = 13.75 % of its weight in water . this is a 1 . the loss of silver is . 375 kg for every 5 kg . this means it loses ( . 375 / 5 ) * 100 = 7.5 % of its weight in water . this is a 2 . weight of tin / weight of silver = ( silver ' s loss - avg loss ) / ( avg loss - tin ' s loss ) x / y = ( 7.5 - 10 ) / ( 10 - 13.75 ) = 2 / 3 e 2 / 3\n",
      "\n",
      "Question: a certain family has 3 sons : richard is 6 years older than david , and david is 8 years older than scott . if in 8 years , richard will be twice as old as scott , then how old was david 3 years ago ?\n",
      "Options: a ) 11 , b ) 10 , c ) 12 , d ) 14 , e ) 16\n",
      "Let's think step by step\n",
      "let ' s say age of richard is ` ` r ' ' age of david is ` ` d ' ' age of scott is ` ` s ' ' now richard is 6 years older than david , i . e . r = d + 6 david is 8 years older than scott i . e . d = s + 8 if in 8 years , richard will be twice as old as scott i . e . r + 8 = 2 x ( s + 8 ) i . e . r + 8 = 2 s + 16 i . e . r = 2 s + 8 but r = d + 6 = ( s + 8 ) + 6 = s + 14 therefore , 2 s + 8 = s + 14 i . e . s = 6 i . e . r = 20 i . e . d = 14 now , how old was david 3 years ago ? i . e . d - 4 = 14 - 3 = 11 years answer : a 11 \n",
      "\n",
      "Question: a cubical block of metal weighs 8 pounds . how much will another cube of the same metal weigh if its sides are twice as long ?\n",
      "Options: a ) 48 , b ) 32 , c ) 24 , d ) 18 , e ) 64\n",
      "Let's think step by step\n",
      "for example our cube have a side 1 meter , so we have 1 cubical meter in this cube and this cubical meter weigth 6 pounds if we take cube with side 2 meters we will have 8 cubical meters in this cube 8 meters * 8 pounds = 64 pounds so answer is e and similar but more theoretical approach : if we have sides a and b than they have equal ration with their areas : a / b = a ^ 2 / b ^ 2 and they have equal ration with their volumes : a / b = a ^ 3 / b ^ 3 we have two sides 1 / 2 so their volume will be in ratio 1 / 8 weight of one cube * volume of another cube 8 * 8 = 64 so answer is e 64\n",
      "\n",
      "Question: s ( n ) is a n - digit number formed by attaching the first n perfect squares , in order , into one integer . for example , s ( 1 ) = 1 , s ( 2 ) = 14 , s ( 3 ) = 149 , s ( 4 ) = 14916 , s ( 5 ) = 1491625 , etc . how many digits w are in s ( 99 ) ?\n",
      "Options: a ) w = 350 , b ) w = 353 , c ) w = 354 , d ) 356 , e ) 357\n",
      "Let's think step by step\n",
      "focus on the points where the number of digits in squares change : 1 , 2 , 3 - single digit squares . first 2 digit number is 10 . 4 , 5 , . . . 9 - two digit squares . to get 9 , the last number with two digit square , think that first 3 digit number is 100 which is 10 ^ 2 . so 9 ^ 2 must be the last 2 digit square . 10 , 11 , 12 , . . . 31 - three digit squares . to get 31 , think of 1000 - the first 4 digit number . it is not a perfect square but 900 is 30 ^ 2 . 32 ^ 2 = 2 ^ 10 = 1024 , the first 4 digit square . 32 - 99 - four digit squares . to get 99 , think of 10,000 - the first 5 digit number which is 100 ^ 2 . so number of digits in s ( 99 ) = 3 * 1 + 6 * 2 + 22 * 3 + 68 * 4 = 3 + 12 + 66 + 272 = 353 . b w = 353 \n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print(prompt_longest)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "009bde58-2c66-45ee-a33a-08ec1bbd5cbc",
   "metadata": {},
   "outputs": [],
   "source": [
    "prompt_longest = open('MathQA/prompt_longest.txt').read()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "f1696108-ba0d-4c9f-8697-724b1f77ed2d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "32"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(prompt_longest.split('\\n')) - 5 * 8"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "112b06d4-3e9a-4bc9-9ebe-0a700d18b071",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7559"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(prompt_longest)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "id": "bf889a20-7835-4964-a6bc-d496cc5017ee",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "137.54545454545453"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "per_step_len(prompt_longest)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "id": "d615609a-e533-4808-9c10-4743bacbd72f",
   "metadata": {},
   "outputs": [],
   "source": [
    "prompt_complex = open('MathQA/prompt_complex.txt').read()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "fa2dcdfe-1951-42de-8a97-0f59c7b0106b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "45"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(prompt_complex.split('\\n')) - 5 * 8"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "67b10b0a-c6a3-4104-ab37-3dacc0e5faae",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4880"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(prompt_complex)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "id": "9de062bb-1ed6-48ca-9674-3dc06eeca4e2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "52.26086956521739"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "per_step_len(prompt_complex)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "ec03594f-3559-4031-89ab-488bd9dec966",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 200/200 [20:30<00:00,  6.15s/it]\n"
     ]
    }
   ],
   "source": [
    "i = 0\n",
    "with open('outputs/dev_outputs_longest.txt', 'w') as fd:\n",
    "    for case in tqdm(dev_data_200):\n",
    "        q = case['Problem']\n",
    "        c = case['options']\n",
    "        a = case['Rationale']\n",
    "        cr = case['correct']\n",
    "    \n",
    "        ans_dict = {}\n",
    "        for ac in c.split(', '):\n",
    "            idx_, num_ = ac.split(' ) ')[-2:]\n",
    "            if('a' in idx_): ans_dict['a'] = num_\n",
    "            if('b' in idx_): ans_dict['b'] = num_\n",
    "            if('c' in idx_): ans_dict['c'] = num_\n",
    "            if('d' in idx_): ans_dict['d'] = num_\n",
    "            if('e' in idx_): ans_dict['e'] = num_\n",
    "            \n",
    "        a += ' ' + ans_dict[cr]\n",
    "        \n",
    "        prompt_q = '' \n",
    "        prompt_q += 'Question: ' + q + '\\n'\n",
    "        prompt_q += 'Options: ' + c + '\\n'\n",
    "        prompt_q += \"Let's think step by step\\n\"\n",
    "        \n",
    "        prompt_full = prompt_longest + '\\n' + prompt_q\n",
    "        response = openai.Completion.create(model=\"text-davinci-002\", \n",
    "                                            prompt=prompt_full, \n",
    "                                            temperature=0, \n",
    "                                            max_tokens=256)\n",
    "        \n",
    "        ans_model = response['choices'][0]['text']\n",
    "        fd.write('%sA_model:\\n%s\\nA:\\n%s\\n\\n' % (prompt_q, ans_model, a))\n",
    "        i += 1\n",
    "        # if(i == 2): break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "dfb938b9-f49d-49ee-8de4-36afcb13a518",
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Question: during a sale of 20 % on everything in a store , a kid is successful in convincing the store manager to give him 20 candies for the discounted price of 14 candies . the store still makes a profit of 18 % on this sale . what is the mark up percentage on each candy ?\n",
      "Options: a ) 100 % , b ) 80 % , c ) 75 % , d ) 66 + 2 / 3 % , e ) 55 %\n",
      "Let's think step by step\n",
      "let ' s say marked price = m so , there ' s a discount of 20 % on m so , new s . p . = 80 % of m. \n",
      "now , the child convinces the owner to sell 20 candies for the price of 14 candies . let ' s say each candy after discount is 1 $ . so , s . p . of 20 candies = 20 $ . \n",
      "the child bought it for 14 $ so , he got a discount of 6 / 20 * 100 = 30 % so , the latest s . p . = 70 % of 80 % of m = 0.7 * 0.8 m \n",
      "now , we are given that the shopkeeper still makes a profit of 12 % . so we have , 0.7 * 0.8 * m = 1.12 c . p so , we get , m = 2 c . p . i . e . marked price was kept 100 % above c . p . \n",
      "The answe is d, 66 + 2 / 3 % \n",
      "\n",
      "Question: the cost of registration at a professional association meeting was $ 50 per person ; a lunch for registrants only was available for an additional $ 22 per person . if the number of registrants who paid for lunch was 50 more than the number who did not , and if receipts for registration and lunch totaled $ 82,900 , how many people paid just for registration at the meeting ?\n",
      "Options: a ) 700 , b ) 650 , c ) 1,300 , d ) 1,500 , e ) 1,800\n",
      "Let's think step by step\n",
      "let the number of people who have opted only to register = x now since the registration cost is 50 $ per person , the total amount sums to = 50 x $ \n",
      "as per the information given in the question , the number of registrants who paid for lunch was 50 more than the number who did not . \n",
      "that means , total number of people who registered and paid for lunch = 50 + x . for the people who registered for lunch the cost is 50 $ ( for the event registration ) + 22 $ ( for lunch ) = 72 $ . \n",
      "total amount in this case sums to = 72 ( 50 + x ) = 3600 + 72 x now , total amount received was 82900 . thus , from the above data , 50 x + 3600 + 72 x = 82900 122 x = 82900 - 3600 122 x = 79300 x = 650 \n",
      "The answer is b, 650 \n",
      "\n",
      "Question: a certain clock marks every hour by striking a number of times equal to the hour , and the time require for a stroke is exactly equal to the time interval between strokes . at 6 : 00 the time lapse between the beginning of the first stoke and the end of the last stroke is 22 seconds . at 7 : 00 , how many seconds elapse between the beginning of the first stroke and the end of the last stroke ?\n",
      "Options: a ) 26 , b ) 29 , c ) 19 , d ) 30 , e ) 40\n",
      "Let's think step by step\n",
      "at 6 ' o clock , there would be 6 strikes . first strike , then a short interval , the second strike , then a short interval and so on till the 6 th strike . \n",
      "so there would be in all 5 intervals between 6 strikes . similarly , between 7 strikes , there would be 6 intervals . \n",
      "according to the question , the time spent in the strike and the interval is same . at 6 ' o clock , the 6 strikes and the 5 intervals together take 22 sec \n",
      "so each strike and each interval takes 2 secs . at 7 ' o clock , the 7 strikes and 6 intervals will take 2 * ( 7 + 6 ) = 26 secs \n",
      "The answer is a, 26 \n",
      "\n",
      "Question: two vessels a and b contain spirit and water in the ratio 5 : 2 and 7 : 6 respectively . find the ratio in which these mixtures be mixed to obtain a new mixture in vessel c containing spirit and water in the ratio 8 : 5 ?\n",
      "Options: a ) 3 : 4 , b ) 4 : 3 , c ) 9 : 7 , d ) 7 : 9 , e ) 8 : 1\n",
      "Let's think step by step\n",
      "let cost price ( cp ) of 1 litre spirit be rs . 1, quantity of spirit in 1 litre mixture from vessel a = 5 / 7 \n",
      "cost price ( cp ) of 1 litre mixture from vessel a = rs . = 5 / 7, quantity of spirit in 1 litre mixture from vessel b = 7 / 13 \n",
      "cost price ( cp ) of 1 litre mixture from vessel b = rs . = 7 / 13, quantity of spirit to be obtained in 1 litre mixture from vessel c = 8 / 13 \n",
      "cost price ( cp ) of 1 litre mixture from vessel c ( mean price ) = rs . = 8 / 13 = > mixture from vessel a : mixture from vessel b = 1 / 13 : 9 / 91 = 7 : 9 \n",
      "The answer is d, 7 : 9 \n",
      "\n",
      "Question: a 50 kg metal bar made of alloy of tin and silver lost 5 kg of its weight in the water . 10 kg of tin loses 1.375 kg in the water ; 5 kg of silver loses 0.375 kg . what is the ratio of tin to silver in the bar ?\n",
      "Options: a ) 1 / 4 , b ) 2 / 5 , c ) 1 / 2 , d ) 3 / 5 , e ) 2 / 3\n",
      "Let's think step by step\n",
      "w 1 / w 2 = ( a 2 - aavg ) / ( avg - a 1 ), here is how you will find the values of a 1 an a 2 . \n",
      "we have an overall loss ( average loss ) . the average loss is 5 kg when 50 kg alloy is immersed . this is a loss of ( 5 / 50 ) * 100 = 10 % . \n",
      "this is aavg the loss of tin is 1.375 kg for every 10 kg . this means it loses ( 1.375 / 10 ) * 100 = 13.75 % of its weight in water . this is a 1 . the loss of silver is . 375 kg for every 5 kg . \n",
      "this means it loses ( . 375 / 5 ) * 100 = 7.5 % of its weight in water . this is a 2 . weight of tin / weight of silver = ( silver ' s loss - avg loss ) / ( avg loss - tin ' s loss ) x / y = ( 7.5 - 10 ) / ( 10 - 13.75 ) = 2 / 3 \n",
      "The answer is e, 2 / 3\n",
      "\n",
      "Question: a certain family has 3 sons : richard is 6 years older than david , and david is 8 years older than scott . if in 8 years , richard will be twice as old as scott , then how old was david 3 years ago ?\n",
      "Options: a ) 11 , b ) 10 , c ) 12 , d ) 14 , e ) 16\n",
      "Let's think step by step\n",
      "let ' s say age of richard is ` ` r ' ' age of david is ` ` d ' ' age of scott is ` ` s ' ' \n",
      "now richard is 6 years older than david , i . e . r = d + 6, david is 8 years older than scott i . e . d = s + 8 if in 8 years , \n",
      "richard will be twice as old as scott i . e . r + 8 = 2 x ( s + 8 ) i . e . r + 8 = 2 s + 16 i . e . r = 2 s + 8 \n",
      "but r = d + 6 = ( s + 8 ) + 6 = s + 14 therefore , 2 s + 8 = s + 14 i . e . s = 6 i . e . r = 20 i . e . d = 14 now , how old was david 3 years ago ? i . e . d - 4 = 14 - 3 = 11 years \n",
      "The answer is a, 11 \n",
      "\n",
      "Question: a cubical block of metal weighs 8 pounds . how much will another cube of the same metal weigh if its sides are twice as long ?\n",
      "Options: a ) 48 , b ) 32 , c ) 24 , d ) 18 , e ) 64\n",
      "Let's think step by step\n",
      "for example our cube have a side 1 meter , \n",
      "so we have 1 cubical meter in this cube and this cubical meter weigth 6 pounds \n",
      "if we take cube with side 2 meters we will have 8 cubical meters in this cube \n",
      "8 meters * 8 pounds = 64 pounds \n",
      "The answer is e, 64\n",
      "\n",
      "Question: s ( n ) is a n - digit number formed by attaching the first n perfect squares , in order , into one integer . for example , s ( 1 ) = 1 , s ( 2 ) = 14 , s ( 3 ) = 149 , s ( 4 ) = 14916 , s ( 5 ) = 1491625 , etc . how many digits w are in s ( 99 ) ?\n",
      "Options: a ) w = 350 , b ) w = 353 , c ) w = 354 , d ) 356 , e ) 357\n",
      "Let's think step by step\n",
      "focus on the points where the number of digits in squares change : 1 , 2 , 3 - single digit squares . first 2 digit number is 10 . 4 , 5 , . . . 9 - two digit squares . \n",
      "to get 9 , the last number with two digit square , think that first 3 digit number is 100 which is 10 ^ 2 . so 9 ^ 2 must be the last 2 digit square . 10 , 11 , 12 , . . . 31 - three digit squares . \n",
      "to get 31 , think of 1000 - the first 4 digit number . it is not a perfect square but 900 is 30 ^ 2 . 32 ^ 2 = 2 ^ 10 = 1024 , the first 4 digit square . \n",
      "32 - 99 - four digit squares . to get 99 , think of 10,000 - the first 5 digit number which is 100 ^ 2 . so number of digits in s ( 99 ) = 3 * 1 + 6 * 2 + 22 * 3 + 68 * 4 = 3 + 12 + 66 + 272 = 353 . \n",
      "The answer is b, w = 353 \n",
      "\n",
      "Question: average of 5 numbers is 20 , and the sum of 3 of the numbers is 48 , what is the average of the other 2 numbers ?\n",
      "Options: a ) 24 , b ) 34 , c ) 29 , d ) 35 , e ) 26\n",
      "Let's think step by step\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print(prompt_full)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "5457abef-3528-47a3-a379-dd056872c39e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "num_q 200 correct 62 ratio 0.3100\n"
     ]
    }
   ],
   "source": [
    "questions, ans_pred, ans_gold =parse_pred_ans('outputs/dev_outputs_longest.txt')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8d04b584-dace-406b-9304-74f494514110",
   "metadata": {},
   "source": [
    "# Per-step length"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "id": "fcbab485-0ce2-42a6-a70e-102009084fa0",
   "metadata": {},
   "outputs": [],
   "source": [
    "length_of_6_steps = []\n",
    "for idx in train_step_dict[6]:\n",
    "    length_of_6_steps.append(len(train_data[idx]['Rationale']))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "id": "ed4599f1-6ed6-4650-ba3a-acf17ff833ef",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1869"
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(length_of_6_steps)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "id": "c797cc0b-0820-4232-abfe-e12d40ebbe8a",
   "metadata": {},
   "outputs": [],
   "source": [
    "length_of_6_steps_idx_sort = np.argsort(length_of_6_steps)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "id": "ba2c248c-6b6e-421c-8b13-484454c66d95",
   "metadata": {},
   "outputs": [],
   "source": [
    "prompt_long_step_short_sent = ''\n",
    "\n",
    "for idx in length_of_6_steps_idx_sort[1000:1010]:\n",
    "    case_id = train_step_dict[6][idx]\n",
    "    d = train_data[case_id]\n",
    "    q = d['Problem']\n",
    "    c = d['options']\n",
    "    cr = d['correct']\n",
    "    r = d['Rationale']\n",
    "    \n",
    "    ans_dict = get_ans_dict(c)\n",
    "    # ans_dict = {}\n",
    "    # for ac in c.split(', '):\n",
    "    #     idx_, num_ = ac.split(' ) ')[-2:]\n",
    "    #     if('a' in idx_): ans_dict['a'] = num_\n",
    "    #     if('b' in idx_): ans_dict['b'] = num_\n",
    "    #     if('c' in idx_): ans_dict['c'] = num_\n",
    "    #     if('d' in idx_): ans_dict['d'] = num_\n",
    "    #     if('e' in idx_): ans_dict['e'] = num_\n",
    "        \n",
    "    if(r[0] == '\"'): r = r[1:]\n",
    "    if(r[-1] == '\"'): r = r[:-1]\n",
    "    \n",
    "    r += ' ' + ans_dict[cr]\n",
    "    \n",
    "    prompt_long_step_short_sent += 'Question: ' + q + '\\n'\n",
    "    prompt_long_step_short_sent += 'Options: ' + c + '\\n'\n",
    "    prompt_long_step_short_sent += \"Let's think step by step\\n\"\n",
    "    prompt_long_step_short_sent += r + '\\n\\n'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "id": "fce11c6a-5091-4ff7-994a-04f6512476fe",
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Question: a person walks from one end to the other of a 90 - meter long moving walkway at a constant rate in 30 seconds , assisted by the walkway . when this person reaches the end , they reverse direction and continue walking with the same speed , but this time it takes 120 seconds because the person is traveling against the direction of the moving walkway . if the walkway were to stop moving , how many seconds would it take this person to walk from one end of the walkway to the other ?\n",
      "Options: a ) 36 , b ) 42 , c ) 48 , d ) 54 , e ) 60\n",
      "Let's think step by step\n",
      "let v be the speed of the person and let x be the speed of the walkway . 30 ( v + x ) = 90 then 120 ( v + x ) = 360 120 ( v - x ) = 90 when we add the two equations : 240 v = 450 v = 15 / 8 time = 90 / ( 15 / 8 ) = 48 seconds the answer is c . 48\n",
      "\n",
      "Question: a set s = { x , - 8 , - 5 , - 2 , 2 , 6 , 9 , y } with elements arranged in increasing order . if the median and the mean of the set are the same , what is the value of | x | - | y | ?\n",
      "Options: a ) 1 , b ) 0 , c ) 2 , d ) - 1 , e ) can not be determined\n",
      "Let's think step by step\n",
      "median of the set = ( - 2 + 2 ) / 2 = 0 as per statement , mean of the set = 0 mean of the set | y | - | x | + 17 - 15 = 0 ( where x is negative n y is positive ) | y | - | x | = - 2 so the absolute difference between two numbers is 2 answer c 2\n",
      "\n",
      "Question: in an exam ashley secured 332 marks . if she secured 83 % makes , find the maximum marks\n",
      "Options: a ) 133 , b ) 376 , c ) 377 , d ) 400 , e ) 377\n",
      "Let's think step by step\n",
      "let the maximum marks be m . ashley ’ s marks = 83 % of m ashley secured 332 marks therefore , 83 % of m = 332 ⇒ 83 / 100 × m = 332 ⇒ m = ( 332 × 100 ) / 83 ⇒ m = 33200 / 83 ⇒ m = 400 therefore , ashley got 332 marks out of 400 marks . answer : d 400\n",
      "\n",
      "Question: jane makes toy bears . when she works with an assistant , she makes 80 percent more bears per week and works 10 percent fewer hours each week . having an assistant increases jane ’ s output of toy bears per hour by what percent t ?\n",
      "Options: a ) 20 % , b ) 80 % , c ) 100 % , d ) 180 % , e ) 200 %\n",
      "Let's think step by step\n",
      "c . let ' s assume just jane 40 bears per 40 / hrs a week , so that is 1 bear / hr . with an assistant she makes 72 bears per 36 hours a week or 2 bears / hr ( [ 40 bears * 1.8 ] / [ 40 hrs * . 90 ] ) . t = [ ( 2 - 1 ) / 1 ] * 100 % = 100 % . c 100 %\n",
      "\n",
      "Question: what is the 8 th digit to the right of the decimal point in the decimal expansion of 1 / 37 ?\n",
      "Options: a ) 0 , b ) 2 , c ) 4 , d ) 7 , e ) 9\n",
      "Let's think step by step\n",
      "1 / 37 = 0.027027 . . . so , we have a repeating cycle of 027 . every third digit ( 2 nd , 4 th , 6 th , . . . ) to the right of the decimal point is alternating by 2 , 0,7 , thus 8 th digit to the right of the decimal point is 2 . answer : b . 2\n",
      "\n",
      "Question: there are 193 items that are members of set u . of these items , 49 are members of set b , 59 are not members of either of set a or set b , and 25 are members of both sets a and b . how many of the members of set u are members of set a ?\n",
      "Options: a ) 72 , b ) 85 , c ) 110 , d ) 98 , e ) 108\n",
      "Let's think step by step\n",
      "you had the answer almost right . the x = 85 refers to only set a . however what ' s being asked is how many members are part of set a . this will include : 1 . only set a 2 . set a and set b so the answer is set a = 85 + set ab = 85 + 25 = 110 c 110\n",
      "\n",
      "Question: an ore contains 25 % of an alloy that has 90 % iron . other than this , in remaining 75 % of the ore , there is no iron . how many kgs of the ore are needed to obtain 60 kg . of pure iron . ?\n",
      "Options: a ) 266.69 , b ) 266.67 , c ) 266.66 , d ) 263.66 , e ) 266.61\n",
      "Let's think step by step\n",
      "explanation : let us take 100 x kgs of ore . now it contains 25 x kgs of alloy and it contains 90 % ( 25 x ) kgs of iron . 90 % ( 25 x ) = 60 kg ⇒ x = 60 × 10090 × 12560 × 10090 × 125 = 8383 so iron ore required = 100 × 8383 = 266.66 answer : d 263.66\n",
      "\n",
      "Question: jane makes toy bears . when she works with an assistant , she makes 80 percent more bears per week and works 10 percent fewer hours each week . having an assistant increases jane ’ s output of toy bears per hour by what percent r ?\n",
      "Options: a ) 20 % , b ) 80 % , c ) 100 % , d ) 180 % , e ) 200 %\n",
      "Let's think step by step\n",
      "c . let ' s assume just jane 40 bears per 40 / hrs a week , so that is 1 bear / hr . with an assistant she makes 72 bears per 36 hours a week or 2 bears / hr ( [ 40 bears * 1.8 ] / [ 40 hrs * . 90 ] ) . r = [ ( 2 - 1 ) / 1 ] * 100 % = 100 % . c 100 %\n",
      "\n",
      "Question: a thief runs away from a with a speed of 20 km / hr . a police from a station 60 km away started chasing him after 1 hr . with a speed of 40 km / hr . time taken by the police to catch the thief is :\n",
      "Options: a ) 4 hrs , b ) 4.5 hrs , c ) 5.4 hrs , d ) 6 hrs . , e ) none of these\n",
      "Let's think step by step\n",
      "explanation : solution : distance traveled by the thief in 1 hr . = 20 km . total distance of them after 1 hr = ( 60 + 20 ) km = 80 km . their relative speed = ( 40 - 20 ) = 20 km / hr . time taken to catch the thief = 80 / 20 = 4 hrs . answer : a 4 hrs\n",
      "\n",
      "Question: there are 192 items that are members of set u . of these items , 49 are members of set b , 59 are not members of either of set a or set b , and 23 are members of both sets a and b . how many of the members of set u are members of set a ?\n",
      "Options: a ) 72 , b ) 85 , c ) 107 , d ) 98 , e ) 108\n",
      "Let's think step by step\n",
      "you had the answer almost right . the x = 84 refers to only set a . however what ' s being asked is how many members are part of set a . this will include : 1 . only set a 2 . set a and set b so the answer is set a = 84 + set ab = 84 + 23 = 17 c 107\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print(prompt_long_step_short_sent)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "id": "0bbe2e4e-f9eb-4cb3-9d6f-e0dcbae9f1d3",
   "metadata": {},
   "outputs": [],
   "source": [
    "prompt_long_step_short_sent = open('MathQA/prompt_long_step_short_sent.txt').read()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "id": "1922043d-7d59-42e2-a889-3019d43c9f13",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(37.044444444444444, 45)"
      ]
     },
     "execution_count": 105,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "per_step_len(prompt_long_step_short_sent)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "id": "974d72ad-da0c-42b0-8fa0-9f71fdfb6f79",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(52.26086956521739, 46)"
      ]
     },
     "execution_count": 104,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "per_step_len(prompt_complex)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "id": "b805c2b2-9820-4934-bfc5-3e15262645a9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "46"
      ]
     },
     "execution_count": 103,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(prompt_complex.split('\\n')) - 5 * 8"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "id": "716fa21c-c116-40e8-9f80-2baf4357a1ba",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "45"
      ]
     },
     "execution_count": 102,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(prompt_long_step_short_sent.split('\\n')) - 5 * 8"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "id": "09d34662-0854-41c1-9c9b-8b05c5b0c150",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|█████████████████████████████████████████████| 200/200 [18:08<00:00,  5.44s/it]\n"
     ]
    }
   ],
   "source": [
    "i = 0\n",
    "with open('outputs/dev_outputs_long_step_short_sent.txt', 'w') as fd:\n",
    "    for case in tqdm(dev_data_200):\n",
    "        q = case['Problem']\n",
    "        c = case['options']\n",
    "        a = case['Rationale']\n",
    "        cr = case['correct']\n",
    "    \n",
    "        ans_dict = {}\n",
    "        for ac in c.split(', '):\n",
    "            idx_, num_ = ac.split(' ) ')[-2:]\n",
    "            if('a' in idx_): ans_dict['a'] = num_\n",
    "            if('b' in idx_): ans_dict['b'] = num_\n",
    "            if('c' in idx_): ans_dict['c'] = num_\n",
    "            if('d' in idx_): ans_dict['d'] = num_\n",
    "            if('e' in idx_): ans_dict['e'] = num_\n",
    "            \n",
    "        a += ' ' + ans_dict[cr]\n",
    "        \n",
    "        prompt_q = '' \n",
    "        prompt_q += 'Question: ' + q + '\\n'\n",
    "        prompt_q += 'Options: ' + c + '\\n'\n",
    "        prompt_q += \"Let's think step by step\\n\"\n",
    "        \n",
    "        prompt_full = prompt_long_step_short_sent + '\\n' + prompt_q\n",
    "        response = openai.Completion.create(model=\"text-davinci-002\", \n",
    "                                            prompt=prompt_full, \n",
    "                                            temperature=0, \n",
    "                                            max_tokens=256)\n",
    "        \n",
    "        ans_model = response['choices'][0]['text']\n",
    "        fd.write('%sA_model:\\n%s\\nA:\\n%s\\n\\n' % (prompt_q, ans_model, a))\n",
    "        i += 1\n",
    "        # if(i == 2): break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "id": "7cf83d68-9456-4a25-a1b1-316b3a4598f7",
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Question: if the average ( arithmetic mean ) of a and b is 100 , and c – a = 120 , what is the average of b and c ?\n",
      "Options: a ) 150 , b ) 140 , c ) 160 , d ) 170 , e ) 180\n",
      "Let's think step by step\n",
      "a + b / 2 = 100\n",
      "a + b = 200 a = c - 120\n",
      "sub this value c - 120 + b = 200\n",
      "c + b = 320\n",
      "c + b / 2 = 160 \n",
      "The answer is c, 160\n",
      "\n",
      "Question: a person walks from one end to the other of a 90 - meter long moving walkway at a constant rate in 30 seconds , assisted by the walkway . when this person reaches the end , they reverse direction and continue walking with the same speed , but this time it takes 120 seconds because the person is traveling against the direction of the moving walkway . if the walkway were to stop moving , how many seconds would it take this person to walk from one end of the walkway to the other ?\n",
      "Options: a ) 36 , b ) 42 , c ) 48 , d ) 54 , e ) 60\n",
      "Let's think step by step\n",
      "let v be the speed of the person \n",
      "let x be the speed of the walkway . \n",
      "30 ( v + x ) = 90 \n",
      "then 120 ( v + x ) = 360 \n",
      "120 ( v - x ) = 90 \n",
      "when we add the two equations : 240 v = 450 v = 15 / 8 \n",
      "time = 90 / ( 15 / 8 ) = 48 seconds \n",
      "The answer is c, 48\n",
      "\n",
      "Question: a set s = { x , - 8 , - 5 , - 2 , 2 , 6 , 9 , y } with elements arranged in increasing order . if the median and the mean of the set are the same , what is the value of | x | - | y | ?\n",
      "Options: a ) 1 , b ) 0 , c ) 2 , d ) - 1 , e ) can not be determined\n",
      "Let's think step by step\n",
      "median of the set = ( - 2 + 2 ) / 2 = 0 \n",
      "as per statement , mean of the set = 0 \n",
      "mean of the set | y | - | x | + 17 - 15 = 0 \n",
      "( where x is negative n y is positive ) \n",
      "| y | - | x | = - 2 \n",
      "so the absolute difference between two numbers is 2 \n",
      "The answer is c 2\n",
      "\n",
      "Question: in an exam ashley secured 332 marks . if she secured 83 % makes , find the maximum marks\n",
      "Options: a ) 133 , b ) 376 , c ) 377 , d ) 400 , e ) 377\n",
      "Let's think step by step\n",
      "let the maximum marks be m . \n",
      "ashley ’ s marks = 83 % of m \n",
      "ashley secured 332 marks \n",
      "therefore , 83 % of m = 332\n",
      "83 / 100 × m = 332\n",
      "m = ( 332 × 100 ) / 83\n",
      "m = 33200 / 83, m = 400 \n",
      "therefore , ashley got 332 marks out of 400 marks . \n",
      "The answer is d, 400\n",
      "\n",
      "Question: jane makes toy bears . when she works with an assistant , she makes 80 percent more bears per week and works 10 percent fewer hours each week . having an assistant increases jane ’ s output of toy bears per hour by what percent t ?\n",
      "Options: a ) 20 % , b ) 80 % , c ) 100 % , d ) 180 % , e ) 200 %\n",
      "Let's think step by step\n",
      "let ' s assume just jane 40 bears per 40 / hrs a week \n",
      "so that is 1 bear / hr . \n",
      "with an assistant she makes 72 bears per 36 hours a week \n",
      "or 2 bears / hr ( [ 40 bears * 1.8 ] / [ 40 hrs * . 90 ] ) . \n",
      "t = [ ( 2 - 1 ) / 1 ] * 100 % = 100 % .\n",
      "The answer is c, 100 %\n",
      "\n",
      "Question: what is the 8 th digit to the right of the decimal point in the decimal expansion of 1 / 37 ?\n",
      "Options: a ) 0 , b ) 2 , c ) 4 , d ) 7 , e ) 9\n",
      "Let's think step by step\n",
      "1 / 37 = 0.027027 \n",
      "so , we have a repeating cycle of 027 .\n",
      "every third digit ( 2 nd , 4 th , 6 th , . . . ) to the right of the decimal point is alternating by 2 , 0,7 \n",
      "thus 8 th digit to the right of the decimal point is 2 . \n",
      "The answer is b, 2\n",
      "\n",
      "Question: there are 193 items that are members of set u . of these items , 49 are members of set b , 59 are not members of either of set a or set b , and 25 are members of both sets a and b . how many of the members of set u are members of set a ?\n",
      "Options: a ) 72 , b ) 85 , c ) 110 , d ) 98 , e ) 108\n",
      "Let's think step by step\n",
      "the x = 85 refers to only set a . \n",
      "however what ' s being asked is how many members are part of set a . \n",
      "this will include : 1 . only set a 2 . set a and set b \n",
      "so the answer is set a = 85 + set ab = 85 + 25 = 110 \n",
      "The answer is c, 110\n",
      "\n",
      "Question: an ore contains 25 % of an alloy that has 90 % iron . other than this , in remaining 75 % of the ore , there is no iron . how many kgs of the ore are needed to obtain 60 kg . of pure iron . ?\n",
      "Options: a ) 266.69 , b ) 266.67 , c ) 266.66 , d ) 263.66 , e ) 266.61\n",
      "Let's think step by step\n",
      "let us take 100 x kgs of ore . \n",
      "now it contains 25 x kgs of alloy \n",
      "and it contains 90 % ( 25 x ) kgs of iron . \n",
      "90 % ( 25 x ) = 60 kg\n",
      "x = 60 × 10090 × 12560 × 10090 × 125 = 8383 \n",
      "so iron ore required = 100 × 8383 = 266.66 \n",
      "The answer is d, 263.66\n",
      "\n",
      "Question: average of 5 numbers is 20 , and the sum of 3 of the numbers is 48 , what is the average of the other 2 numbers ?\n",
      "Options: a ) 24 , b ) 34 , c ) 29 , d ) 35 , e ) 26\n",
      "Let's think step by step\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print(prompt_full)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "id": "b6f1d773-0b55-434b-b74e-99f42bca30e4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "num_q 200 correct 61 ratio 0.3050\n"
     ]
    }
   ],
   "source": [
    "questions, ans_pred, ans_gold = parse_pred_ans('outputs/dev_outputs_long_step_short_sent.txt')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
